package com.mqtt.ctwingmqtt.controller;

import com.mqtt.ctwingmqtt.domain.PageResult;
import com.mqtt.ctwingmqtt.domain.ResponseResult;
import com.mqtt.ctwingmqtt.domain.dto.DtuDto;
import com.mqtt.ctwingmqtt.domain.vo.DtuVO;
import com.mqtt.ctwingmqtt.service.DtuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "DTU接口")
@RestController
@Slf4j
@RequestMapping("/dtu")
public class DtuController {
    @Autowired
    private DtuService dtuService;

    @ApiOperation("添加DTU")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody DtuDto dtuDto) {
        log.info("添加DTU");
        Boolean result = dtuService.add(dtuDto);
        return result ? ResponseResult.success("添加成功") : ResponseResult.error("添加失败");
    }

    @ApiOperation("删除DTU")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam Long id) {
        log.info("删除DTU");
        Boolean result = dtuService.delete(id);
        return result ? ResponseResult.success("删除成功") : ResponseResult.error("删除失败");
    }

    @ApiOperation("批量删除DTU")
    @PostMapping("/deleteAll")
    public ResponseResult deleteAll(@RequestParam String ids) {
        log.info("批量删除DTU");
        String[] idArray = ids.split(",");
        for (String s : idArray) {
            Long id = Long.parseLong(s);
            Boolean result = dtuService.delete(id);
            if (!result) {
                return ResponseResult.error("删除失败");
            }
        }
        return ResponseResult.success("删除成功");
    }

    @ApiOperation("修改DTU")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody DtuDto dtuDto) {
        log.info("修改DTU");
        Boolean result = dtuService.update(dtuDto);
        return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
    }

    @ApiOperation("查询所有DTU")
    @GetMapping("/selectAll")
    public ResponseResult<List<DtuVO>> selectAll() {
        log.info("查询所有DTU");
        return ResponseResult.success(dtuService.selectAll());
    }

    @ApiOperation("查询单个DTU")
    @GetMapping("/selectOne")
    public ResponseResult<DtuVO> selectOne(@RequestParam Long id) {
        log.info("查询单个DTU");
        return ResponseResult.success(dtuService.selectOne(id));
    }
    
    @ApiOperation("分页条件查询")
    @GetMapping("/selectPage")
    public ResponseResult selectPage(@RequestParam(defaultValue = "1") Long pageNum,
                                                 @RequestParam(defaultValue = "10") Long pageSize,
                                                 @RequestParam String dtuCode,
                                                 @RequestParam String dtuName,
                                                 @RequestParam Long dtuId) {
        log.info("分页条件查询");
        PageResult<DtuVO> dtuVOPageResult = dtuService.selectPage(pageNum, pageSize, dtuCode, dtuName, dtuId);
        return ResponseResult.success(dtuVOPageResult);

    }
}